<?php

namespace App\Http\Controllers\Wap;

use App\Http\Requests;
use App\Http\Util\Result;
use App\Model\Receiver;
use App\Model\Member;
use App\Model\OrderDetail;
use App\Model\PromoteDerate;
use App\Model\PromoteGive;
use App\Http\Util\PageHelper;
use App\Model\OrderList;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class OrderController extends Controller
{

    public function __construct(Request $request){
        $this->request = $request;
//        $this->openid = session('wechat_user')['id'];
        $this->openid = 'oiYTrwfH-VLzRn8OkLsn_knKv3qM';
        if (!empty($this->openid)) {
            $this->memberId = Member::where('openid',$this->openid)->pluck('id');
        }
        $this->limit = 6;
    }

    /**
     * 初次提交订单
     * czs
     */
    public function submitOrder() {
        $receiver = DB::table('receivers')->where('member_id',$this->memberId)->count();
        if (empty($receiver)) {
            return [
                'msg'=>"请先填写收货地址！",
                'success'=>false,
                'type'=>1
            ];
        }
        $postData = $this->request->get('goods');
        $res = OrderDetail::readyOrder($postData,$this->memberId);
        return $res->getResult();
    }

    /**
     * 立即购买
     * czs
     */
    public function inputOrder() {
        if (empty($this->openid)) {
            return [
                'success'=>false,
                'type'=>9
            ];
        }
        $receiver = DB::table('receivers')->where('member_id',$this->memberId)->count();
        if (empty($receiver)) {
            return [
                'msg'=>"请先填写收货地址！",
                'success'=>false,
                'type'=>1
            ];
        }
        $goodId = $this->request->get('goodId');
        $num = $this->request->get('num');
        $res = OrderDetail::inputOrder($goodId,$num,$this->memberId);
        return $res->getResult();
    }

    /**
     * 确认订单
     * czs
     */
    public function orderCommit(){
        $res = OrderDetail::commitOrder($this->memberId)->getData();
        $goods = OrderDetail::goodsDetail($res->id)->getData();
        //满减活动
        $deratePrice = OrderDetail::deratePrice($res->id)->getData();
        $canDeratePrice = 0;
        foreach ($deratePrice as $v) {
            $canDeratePrice += $v->price*$v->num;
        }
        if ($canDeratePrice) {
            $derate = PromoteDerate::get();
            $effect = 0;
            foreach ($derate as $v) {
                if ($canDeratePrice>=$v->effect_amount) {
                    if ($v->effect_amount>=$effect) {
                        $effect = $v->effect_amount;
                        $cheap = $v->cheap_amount;
                    }
                }
            }
        }
        //满送活动
        $givePrice = OrderDetail::givePrice($res->id)->getData();
        $canGivePrice = 0;
        foreach ($givePrice as $v) {
            $canGivePrice += $v->price*$v->num;
        }
        if ($canGivePrice) {
            $give = PromoteGive::get();
            $effect2 = 0;
            foreach ($give as $v) {
                if ($canGivePrice>=$v->effect_amount) {
                    if ($v->effect_amount>=$effect2) {
                        $effect2 = $v->effect_amount;
                        $num = $v->num;
                    }
                }
            }
        }
        //总价
        $allPrice = OrderDetail::allPrice($res->id)->getData();
        $all = 0;
        foreach ($allPrice as $v) {
            $all += $v->price*$v->num;
        }
        return view('wap.order.orderCommit')
            ->with('res',$res)
            ->with('goods',$goods)
            ->with('effect',!empty($effect)?$effect:"")
            ->with('cheap',!empty($cheap)?$cheap:"")
            ->with('effect2',!empty($effect2)?$effect2:"")
            ->with('num',!empty($num)?$num:"")
            ->with('all',$all)
            ->with('memberId',$this->memberId);
    }

    //提交订单
    public function finalOrder() {
        $typeId = $this->request->get('typeId');
        $res = OrderDetail::commitOrder($this->memberId)->getData();
        if (empty($res)) {
            $confirm =  Result::failure()->setMsg("勿重复提交！");
            return $confirm->getResult();
        }
        $final = OrderDetail::finalOrder($res->id,$typeId);
        return $final->getResult();
    }

    /**
     * 订单列表
     * czs
     */
    public function orderList() {
//        OrderList::deleteDue($this->memberId);
        $res = OrderList::wapAllOrder($this->memberId,$this->limit)->getData();
        return view('wap.order.orderList')
            ->with('res',$res)
            ->with('more',$res->hasMorePages()?1:0);
    }
    public function orderListAjax() {
        $res = OrderList::wapAllOrder($this->memberId,$this->limit)->getData();
        $html = view('wap.order.orderList-ajax')->with('res',$res)->render();
        $data = [
            'html'=>$html,
            'more'=>$res->hasMorePages()?1:0
        ];
        return $data;
    }

    /**
     * 等待付款列表
     * czs
     */
    public function forPayList() {
//        OrderList::deleteDue($this->memberId);
        $res = orderList::wapForPayList($this->memberId,$this->limit)->getData();
        return view('wap.order.forPayList')
            ->with('res',$res)
            ->with('more',$res->hasMorePages()?1:0);
    }
    public function payListAjax() {
        $res = OrderList::wapForPayList($this->memberId,$this->limit)->getData();
        $html = view('wap.order.payList-ajax')->with('res',$res)->render();
        $data = [
            'html'=>$html,
            'more'=>$res->hasMorePages()?1:0
        ];
        return $data;
    }

    /**
     * 等待发货列表
     * czs
     */
    public function forDeliveList() {
        $res = orderList::wapForDeliveList($this->memberId,$this->limit)->getData();
        return view('wap.order.forDeliveList')
            ->with('res',$res);
    }
    public function deliveListAjax() {
        $res = OrderList::wapForDeliveList($this->memberId,$this->limit)->getData();
        $html = view('wap.order.deliveList-ajax')->with('res',$res)->render();
        $data = [
            'html'=>$html,
            'more'=>$res->hasMorePages()?1:0
        ];
        return $data;
    }

    /**
     * 等待收货列表
     * czs
     */
    public function forReceiveList() {
        $res = orderList::wapForReceiveList($this->memberId,$this->limit)->getData();
        return view('wap.order.forReceiveList')
            ->with('res',$res);
    }
    public function receiveListAjax() {
        $res = OrderList::wapForReceiveList($this->memberId,$this->limit)->getData();
        $html = view('wap.order.receiveList-ajax')->with('res',$res)->render();
        $data = [
            'html'=>$html,
            'more'=>$res->hasMorePages()?1:0
        ];
        return $data;
    }

    /**
     * wap在线付款
     * czs
     */
    public function payWay($id){
        $payData = OrderList::onlineWapPay($id)->getData();
        return view('wap.order.payWay')
            ->with('payData',$payData);
    }

    /**
     * wap待发货
     * czs
     */
    public function forDelive($id){
        $res = OrderList::wapForDelive($id)->getData();
        $detail = OrderDetail::goodsDetail($res->id)->getData();
        return view('wap.order.forDelive')
            ->with('res',$res)
            ->with('detail',$detail);
    }

    /**
     * 待付款详情
     * czs
     */
    public function forPay($id){
        $orderTime = DB::table('order_lists')->where('id',$id)->pluck('order_time');
        $timeLength = DB::table('order_settings')->pluck('due_time_length');
        if ((time()-$orderTime)>$timeLength*60) {
            DB::table('order_details')->where('order_id',$id)->delete();
            DB::table('order_lists')->where('id',$id)->delete();
            return redirect('wap/order/list');
        }
        $res = OrderList::wapForPay($id)->getData();
        $detail = OrderDetail::goodsDetail($res->id)->getData();
        return view('wap.order.forPay')
            ->with('res',$res)
            ->with('detail',$detail);
    }

    /**
     * 待收货详情
     * czs
     */
    public function forReceive($id){
        $res = OrderList::wapForPay($id)->getData();
        //物流
        $flow = OrderDetail::flowList($res->code,$res->waybill);
        $detail = OrderDetail::goodsDetail($res->id)->getData();
        return view('wap.order.forReceive')
            ->with('res',$res)
            ->with('detail',$detail)
            ->with('flow',$flow);
    }

    /**
     * 已完成订单详情
     * czs
     */
    public function complete($id){
        $res = OrderList::wapForPay($id)->getData();
        $res = [];
//        $detail = OrderDetail::goodsDetail($res->id)->getData();
        $detail = [];
        return view('wap.order.complete')
            ->with('res',$res)
            ->with('detail',$detail);
    }

    /**
     * 删除订单
     * czs
     */
    public function deleteOrder() {
        $id = $this->request->get('id');
        $res = OrderList::deleteOrder($id);
        return $res->getResult();
    }

    /**
     * 查看物流
     * czs
     */
    public function orderFlow($id){
        $res = OrderList::wapForPay($id)->getData();
        //物流
        $flow = OrderDetail::flowList($res->code,$res->waybill);
        return view('wap.order.orderFlow')
            ->with('flow',$flow)
            ->with('res',$res);
    }

    /**
     * 确认收货
     * czs
     */
    public function confirmReceive() {
        $id = $this->request->get('id');
        $res = OrderList::confirmReceive($id);
        return $res->getResult();
    }

    public function noorder(){
        return view('wap.order.noorder');
    }
    
}
